import React, {Component} from "react";
import { Space, Table, Select, Divider,Card} from "antd";
const { Column, ColumnGroup } = Table;
import getData from '../../compontens/GetData/getData.jsx'; 

export default class Alpha_Diversity extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            columns:[],
            dataSource:[],
        };
    }

    get_data = (file_Name) => {
      getData(file_Name)
      .then(result => {
        // 在这里处理返回的数据
        this.getTableData(result);
        
      })
      .catch(error => {
        alert('Error');
      });      
    }

    downloadfile = (key) => {
      console.log("download",key);
      const fileUrl = '/example_files/se/processed_results_bracken/diversity/'+key+'.txt'; // 文件的实际路径
      fetch(fileUrl)
        .then(response => response.blob())
        .then(blob => {
          const url = window.URL.createObjectURL(new Blob([blob]));
          const link = document.createElement('a');
          link.href = url;
          link.setAttribute('download', key+'.txt');
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(error => console.log(error));
    }
    componentDidMount() {
      // const kra_result = localStorage.getItem('kra_result');
      const kra_result =  {
        "IDList": ["M0018C_2_month", "M0018C_1_month", "M0018C_3_month", "M0018C_14_day", "M0018C_birth", "M0018M_3_month", "M0018M_gest", "M0018M_birth"],
        "kra_path": "data/Result/1/Kraken_result",
        "result_path": "data/Result/1",
        "sankey_path": "data/Result/1/sankey",
        "krona_path": "data/Result/1/Krona_result",
        "classification_path": "data/Result/1/Kraken_result",
        "diversity_path": "data/Result/1/diversity"
      }
      if(!kra_result){
        return;
      }

      const options = kra_result.IDList.map((item) => {
        return {
          value: item,
          label: item,
        }
      })
      this.setState({
        kra_result: kra_result,
        options : options,
      })
      const file_Name = kra_result.diversity_path + '/alpha.tsv';
      this.get_data(file_Name);
    }
    getTableData = (data) => {
      const rows = data.split('\n');
              const columns = rows.map((row) => row.split('\t'));
              if(columns[columns.length-1].length==1&&columns[columns.length-1][0]==''){
                columns.pop();
              }

              this.setState({dataSource: columns});
  };
    render(){
        const { columns, dataSource } = this.state;
        return(
            <div>
                <Divider orientation="left">Alpha Diversity</Divider>
                      <Table dataSource={dataSource.slice(1)} >
                        {dataSource.length > 0 &&
                                dataSource[0].map((item, index) => (
                                <Column title={item} dataIndex={index} key={item} />
                                ))}
                      
                      </Table>      
            </div>
        )
    }
    
}